package com.abe.libcore.core.screen;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;

import com.abe.libcore.core.clickeffect.ClickEffectTool;
import com.abe.libcore.utils.normal.ConstantCore;
import com.abe.libcore.utils.normal.ToastUtils;

import butterknife.ButterKnife;

public abstract class BaseActivity extends Activity {

    //快捷
    protected abstract int getLayoutRes();

    protected abstract void initContent();

    protected abstract boolean isNeedClickEffect();

    protected abstract boolean isNeedAutoLayout();

    protected abstract float getDesignWidth();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutRes());
        //注入
        ButterKnife.bind(this);
        initContent();
    }

    //*****************************************************************************
    @Override
    public void setContentView(int layoutResID) {
        if (isNeedClickEffect() || isNeedAutoLayout()) {
            View view = View.inflate(this, layoutResID, null);
            this.setContentView(view);
        } else {
            super.setContentView(layoutResID);
        }
    }

    @Override
    public void setContentView(View view) {
        //添加点击效果
        if (isNeedClickEffect()) ClickEffectTool.unifiedAddClickEffect(view);
        //自适应布局
        if (getScale() != 1 && isNeedAutoLayout()) {
            RelayoutTool.relayoutViewHierarchy(view, getScale());
        }
        super.setContentView(view);
    }

    @Override
    public void setContentView(View view, LayoutParams params) {
        //添加点击效果
        if (isNeedClickEffect()) ClickEffectTool.unifiedAddClickEffect(view);
        //自适应布局
        if (getScale() != 1 && isNeedAutoLayout()) {
            RelayoutTool.relayoutViewHierarchy(view, getScale());
            RelayoutTool.scaleLayoutParams(params, getScale());
        }
        super.setContentView(view, params);
    }

    public float getScale() {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        float width = displayMetrics.widthPixels;
        return width / getDesignWidth();
    }
    //*****************************************************************************

    public Context getThisContext() {
        return this;
    }

    public View getContentView() {
        return this.findViewById(android.R.id.content);
    }

    public void gotoActivity(Class<?> activity) {
        Bundle bundle = new Bundle();
        gotoActivity(activity, bundle);
    }

    public void gotoActivity(Class<?> activity, Bundle bundle) {
        Intent intent = new Intent(this, activity);
        if (bundle != null) {
            bundle.putString(ConstantCore.IntentPreviousName, this.getClass().getName());
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public void gotoActivityForResult(Class<?> activity, int requestCode) {
        Bundle bundle = new Bundle();
        gotoActivityForResult(activity, bundle, requestCode);
    }

    public void gotoActivityForResult(Class<?> activity, Bundle bundle, int requestCode) {
        Intent intent = new Intent(this, activity);
        if (bundle != null) {
            bundle.putString(ConstantCore.IntentPreviousName, this.getClass().getName());
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    public void CloseActivity() {
        finish();
    }

    public void CloseActivityForResult(Bundle bundle) {
        Intent intent = new Intent();
        intent.putExtras(bundle);
        // 返回intent
        setResult(RESULT_OK, intent);
        finish();
    }

    public String getEditText(EditText editText) {
        if (editText == null) {
            ToastUtils.show("EditViewNull");
            return "";
        }
        return editText.getText().toString().trim();
    }
}
